using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SpaceGame.Player;
using System.Xml.Linq;
using System.Xml;
using System.Reflection;
using SpaceGame.Enemies;
using SpaceGame.HUD;
using SpaceGame.Particles;
using SpaceGame.Managers;
using SpaceGame.Scenes;

namespace SpaceGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Main : Game
    {
        private GraphicsDeviceManager m_Graphics;
        SpriteBatch m_SpriteBatch;

        private Random m_Random;
        private PlayerShip m_PlayerShip;
        private Texture2D m_BackgroundTexture;
        private Vector2 m_BackgroundLocation = new Vector2(0,0);
        private List<BaseEnemy> m_Enemies;
        private float m_BackgroundMultiplier = 0.8f;
        private MainMenuScene m_MainMenuScene;

        public Main()
        {
            m_Graphics = new GraphicsDeviceManager(this);
            Services.AddService(typeof(GraphicsDeviceManager), m_Graphics);
            Content.RootDirectory = "Content";
            m_Random = new Random();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            m_PlayerShip = new PlayerShip(this);
            m_PlayerShip.Location = new Vector2(200, 200);
            m_PlayerShip.Speed = 3;
            m_PlayerShip.Scale = 1.0f;

            Score score = new Score(this, m_PlayerShip);
            Rights rights = new Rights(this, m_PlayerShip);

            Components.Add(rights);
            Components.Add(score);
            Components.Add(m_PlayerShip);

            m_Enemies = new List<BaseEnemy>();
            for (int i = 0; i < 15; i++)
            {
                BaseEnemy enemy = new BaseEnemy(m_PlayerShip);
                enemy.Texture = Content.Load<Texture2D>("Textures/Enemy/ship");
                enemy.Location = new Vector2(m_Random.Next(0, 800), 0);
                enemy.Speed = 2f;
                enemy.Scale = 1.0f;
                m_Enemies.Add(enemy);
            }
            GameStateManager.GameState = GameState.Paused;

            m_MainMenuScene = new MainMenuScene(this);
            Components.Add(m_MainMenuScene);

            GameStateManager.GameStateChanged += new GameStateManager.GameStateChangedEventHandler(GameStateManager_GameStateChanged);
            base.Initialize();
        }

        void GameStateManager_GameStateChanged(GameState GameState)
        {
            if (GameState == GameState.GameOver)
            {
                GameOverScene gameOverScene = new GameOverScene(this);
                Components.Add(gameOverScene);
            }
            else if (GameState == GameState.Paused)
            {
                m_MainMenuScene.Enabled = true;
                m_MainMenuScene.Visible = true;
            }
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            m_SpriteBatch = new SpriteBatch(GraphicsDevice);
            m_BackgroundTexture = Content.Load<Texture2D>("Textures/Background/bg");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || 
                Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            if (m_Enemies.Count < 15)
            {
                for (int i = 0; i < 15 - m_Enemies.Count; i++)
                {
                    BaseEnemy enemy = new SimpleEnemy(m_PlayerShip);
                    enemy.Texture = Content.Load<Texture2D>("Textures/Enemy/ship");
                    enemy.Location = new Vector2(m_Random.Next(0, 800), 0);
                    enemy.Speed = 2f;
                    enemy.Scale = 1.0f;
                    m_Enemies.Add(enemy);
                }
            }

            m_PlayerShip.Enemies = m_Enemies;

            //m_BackgroundLocation.Y -= m_BackgroundMultiplier;
            if (m_BackgroundLocation.Y + m_BackgroundTexture.Height <= GraphicsDevice.PresentationParameters.BackBufferHeight)
                m_BackgroundMultiplier = -m_BackgroundMultiplier;

            for (int i = 0; i < m_Enemies.Count; i++)
                m_Enemies[i].Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            if (GameStateManager.GameState == GameState.Playing)
            {
                m_SpriteBatch.Begin();

                m_SpriteBatch.Draw(m_BackgroundTexture, m_BackgroundLocation, Color.White);

                for (int i = 0; i < m_Enemies.Count; i++)
                    m_Enemies[i].Draw(m_SpriteBatch);

                m_SpriteBatch.End();
            }

            base.Draw(gameTime);
        }
    }
}
